package any.incubator;

import java.util.List;


public class GenericListCreator<L extends List<T>, T> implements ListCreator<T> {

	private final Class<L> listClass;

	public GenericListCreator(Class<L> listClass) throws IllegalArgumentException, NullPointerException {
		if (!List.class.isAssignableFrom(listClass)) throw new IllegalArgumentException();
		this.listClass = listClass;
		createImpl(0);
	}

	public List<T> create(int initialSize) {
	    return createImpl(initialSize);
    }

	private List<T> createImpl(int initialSize) throws IllegalArgumentException {
		try {
			return listClass.newInstance();
		} catch (Exception e) {
			throw new IllegalArgumentException("Cannot create list of " + listClass, e);
		}
	}

	@Override
	public String toString() {
		return listClass.getName();
	}
	
}
